package com.teng.smartdrawer.parser;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.teng.smartdrawer.BuyActivity;
import com.teng.smartdrawer.HistoryActivity;
import com.teng.smartdrawer.Net.NetApi;
import com.teng.smartdrawer.Net.XHttpTool;
import com.teng.smartdrawer.bean.Component;
import com.teng.smartdrawer.bean.History;
import com.teng.smartdrawer.bean.Module;
import com.teng.smartdrawer.bean.Part;
import com.teng.smartdrawer.bean.Recommend;
import com.teng.smartdrawer.bean.Seek;
import com.teng.smartdrawer.bean.User;
import com.teng.smartdrawer.listener.RequestListener;
import com.teng.smartdrawer.utils.PreferenceUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by wangteng on 2015/11/3.
 */
public final class DataParser {



    //public static final String BASE_SERVER_URL = "http://cabinet.telecomlab.cn/AndroidService/";

    public static final String BASE_SERVER_URL = "http://192.168.0.117/h-cabinet/index.php/home/android_service/";
   // public static final String BASE_SERVER_URL = "http://192.168.0.109/android_service/";

    private DataParser() {

    }

    /**
     * 首页推荐内容
     * @param handler
     *
     */
    public static void getMainRecommend(final Handler handler){

        RequestParams params = new RequestParams(BASE_SERVER_URL+ NetApi.GET_COMPONENT_LIST);
        XHttpTool.getDataByPost(params, new RequestListener() {
            @Override
            public void onSuccess(String result) {
                Message message = handler.obtainMessage();
                message.what = DataConstants.MAIN_RECOMMEND;
                message.obj = result;
            }
        });
    }

    /**
     * 获取零件数据
     * @param handler  http://192.168.0.109/android_service/getComponentList
     */
    public static void getComponentList(final Context context , final Handler handler){
        RequestParams params = new RequestParams(BASE_SERVER_URL+ NetApi.GET_COMPONENT_LIST);
        params.addBodyParameter("want_menu", "2");      //这个参数好像没什么卵用
        XHttpTool.getDataByPost(params, new RequestListener() {
            @Override
            public void onSuccess(String result) {

                try {

                    Log.e("getComponentList" , result);

                    JSONObject jsonObject = new JSONObject(result);

                    JSONArray json = jsonObject.getJSONArray("primary_componentList");

                    TypeToken<List<Part>> token = new TypeToken<List<Part>>(){};

                    List<Part> data =  new Gson().fromJson(json.toString(),token.getType());

                    Message message = handler.obtainMessage();
                    message.what = DataConstants.GET_COMPONENT_SIGN;
                    message.obj = data;

                    handler.sendMessage(message);

                } catch (JSONException e) {
                    Toast.makeText(x.app(), "解析数据错误", Toast.LENGTH_SHORT);
                }

            }
        });
    }

    /**
     * 二级目录
     * @param context
     * @param handler
     * @param id
     */
    public static void getComponentDetailList(final Context context , final Handler handler , String id){
        RequestParams params = new RequestParams(BASE_SERVER_URL+ NetApi.GET_COMPONENT_DETAIL_LIST);
        params.addBodyParameter("id", id);
        XHttpTool.getDataByPost(params, new RequestListener() {
            @Override
            public void onSuccess(String result) {

                try {

                    Log.e("getComponentDetailList",result);

                    JSONObject jsonObject = new JSONObject(result);

                    JSONArray json = jsonObject.getJSONArray("component_list");

                    TypeToken<List<Component>> token = new TypeToken<List<Component>>(){};

                    List<Component> data =  new Gson().fromJson(json.toString(),token.getType());

                    Message message = handler.obtainMessage();
                    message.what = DataConstants.GET_COMPONENT_DETAIL_SIGN;
                    message.obj = data;

                    handler.sendMessage(message);

                } catch (JSONException e) {
                    Toast.makeText(x.app(), "解析数据错误", Toast.LENGTH_SHORT);
                }

            }
        });
    }
    /**
     * 模块详情
     * @param context
     * @param handler
     * @param id
     */
    public static void getModuleDetailList(final Context context , final Handler handler , String id){
        RequestParams params = new RequestParams(BASE_SERVER_URL+ NetApi.GET_MODULE_DETAIL_LIST);
        params.addBodyParameter("id", id);

        XHttpTool.getDataByPost(params, new RequestListener() {
            @Override
            public void onSuccess(String result) {

                try {

                    Log.e("getComponentDetailList",result);

                    JSONObject jsonObject = new JSONObject(result);

                    JSONArray json = jsonObject.getJSONArray("component_list");

                    TypeToken<List<Component>> token = new TypeToken<List<Component>>(){};

                    List<Component> data =  new Gson().fromJson(json.toString(),token.getType());

                    Message message = handler.obtainMessage();
                    message.what = DataConstants.GET_COMPONENT_DETAIL_SIGN;
                    message.obj = data;

                    handler.sendMessage(message);

                } catch (JSONException e) {
                    Toast.makeText(x.app(), "解析数据错误", Toast.LENGTH_SHORT);
                }

            }
        });
    }

    /**
     * 获取模块数据
     * @param handler
     *
     */
    public static void getModuleList(final Context context , final Handler handler){
        RequestParams params = new RequestParams(BASE_SERVER_URL+ NetApi.GET_MODULE_LIST);
//        params.addBodyParameter("want_menu", "2");

        XHttpTool.getDataByPost(params, new RequestListener() {
            @Override
            public void onSuccess(String result) {

                try {

                    JSONObject jsonObject = new JSONObject(result);

                    JSONArray json = jsonObject.getJSONArray("module_list");

                    TypeToken<List<Module>> token = new TypeToken<List<Module>>(){};
                    List<Module> data =  new Gson().fromJson(json.toString(),token.getType());

                    Message message = handler.obtainMessage();
                    message.what = DataConstants.GET_MODULE_SIGN;
                    message.obj = data;

                    handler.sendMessage(message);

                } catch (JSONException e) {
                    Toast.makeText(x.app(), "解析数据错误", Toast.LENGTH_SHORT);
                }
            }
        });
    }





    public static void getSearchContent(final Context context , final Handler handler , final String content){
        RequestParams params = new RequestParams(BASE_SERVER_URL+ NetApi.GET_SEARCH_LIST);
        params.addBodyParameter("keyword", content);



        XHttpTool.getDataByPost(params, new RequestListener() {
            @Override
            public void onSuccess(String result) {
                Message message = handler.obtainMessage();

                Log.e("searchComponent" , content+" ---- >>"+result);

                try {

                    JSONObject jsonObject = new JSONObject(result);

                    int status = jsonObject.optInt("status");

                    if (status == 200){

                        JSONArray cJson = jsonObject.optJSONArray("component_list");

                        List<Recommend> recommendList = new ArrayList<Recommend>();

                        if (cJson != null) {
                            TypeToken<List<Component>> cToken = new TypeToken<List<Component>>() {};
                            List<Component> cData = new Gson().fromJson(cJson.toString(), cToken.getType());
                            recommendList.addAll(cData);
                        }

                        JSONArray mJson = jsonObject.optJSONArray("modular_list");

                        if (mJson != null) {
                            TypeToken<List<Module>> mToken = new TypeToken<List<Module>>() {};
                            List<Module> mData = new Gson().fromJson(mJson.toString(), mToken.getType());
                            recommendList.addAll(mData);
                        }

                        message.what = DataConstants.GET_SEARCH_SIGN;
                        message.obj = recommendList;
                        handler.sendMessage(message);

                    }else {
                        message.what = DataConstants.ERROR;
                        handler.sendMessage(message);
                    }



                } catch (JSONException e) {
                    Toast.makeText(x.app(), "解析数据错误", Toast.LENGTH_SHORT);
                }

            }
        });
    }

    /**
     * 登录
     * @param context
     * @param handler
     * @param account   账号
     * @param password  密码
     */
    public static void getLoginResult(final Context context , final Handler handler , String account , String password){

        RequestParams params = new RequestParams(BASE_SERVER_URL+ NetApi.GET_LOGIN_RESULT);
        params.addBodyParameter("account", account);
        params.addBodyParameter("password", password);

        XHttpTool.getDataByPost(params, new RequestListener() {
            @Override
            public void onSuccess(String result) {
                Message message = handler.obtainMessage();

                //Toast.makeText(x.app() , result , Toast.LENGTH_LONG).show();

                Log.e("hehelohin" , result);

                try {
                    JSONObject json = new JSONObject(result);

                    int status = json.getInt("status");

                    JSONObject userJson = json.getJSONObject("member_list");

                    TypeToken<User> token = new TypeToken<User>(){};

                    User data =  new Gson().fromJson(userJson.toString(), token.getType());

                    data.setStatus(status);

                    message.what = DataConstants.LOGIN_SIGN;
                    message.obj = data;
                    handler.sendMessage(message);

                } catch (JSONException e) {
                    Toast.makeText(x.app(), "解析数据错误", Toast.LENGTH_SHORT).show();
                }

            }
        });


    }

    /**
     * 零器件操作
     * @param context
     * @param handler
     * @param command
     * @param componentId
     * @param number
     */
    public static void componentOperateResult(final Context context , final Handler handler , String command , final String componentId , String number){


        PreferenceUtils preferenceUtils = PreferenceUtils.getInstance(context);

        RequestParams params = new RequestParams(BASE_SERVER_URL+ NetApi.GET_PUT_COMPONENT_DATA_RESULT);
        params.addBodyParameter("command", command);
        params.addBodyParameter("componentId", componentId);
        params.addBodyParameter("number", number);
        params.addBodyParameter("user_name", preferenceUtils.getString(PreferenceUtils.USER_NAME , "admin"));

        XHttpTool.getDataByPost(params, new RequestListener() {
            @Override
            public void onSuccess(String result) {
                Message message = handler.obtainMessage();

                try {
                    JSONObject json = new JSONObject(result);

                    int status = json.getInt("status");

//                    TypeToken<User> token = new TypeToken<User>(){};
//                    User data =  new Gson().fromJson(json.toString(), token.getType());

                    message.what = DataConstants.GET_COMPONENT_OPTION_SIGN;
                    message.obj = status;
                    handler.sendMessage(message);

                } catch (JSONException e) {
                    Toast.makeText(x.app(), "解析数据错误", Toast.LENGTH_SHORT).show();
                }

            }
        });
    }


    /**
     * 添加零件
     * @param context
     * @param handler
     * @param type
     * @param name
     * @param fixed_name
     * @param number
     * @param introduction
     */
    public static void addPart(final Context context , final Handler handler  , final String type , final String name , final String fixed_name , final String number , final String introduction){

        RequestParams params = new RequestParams(BASE_SERVER_URL+ NetApi.GET_PUT_COMPONENT_DATA_RESULT);
        params.addBodyParameter("command", "add_new");
        params.addBodyParameter("tid", type);
        params.addBodyParameter("fixed_num", fixed_name);
        params.addBodyParameter("name", name);
        params.addBodyParameter("introduction", introduction);
        params.addBodyParameter("number", number);

        XHttpTool.getDataByPost(params, new RequestListener() {
            @Override
            public void onSuccess(String result) {
                Message message = handler.obtainMessage();

                try {

                    JSONObject json = new JSONObject(result);

                    int status = json.getInt("status");

                    JSONObject new_component = json.getJSONObject("new_component");

                    TypeToken<Component> token = new TypeToken<Component>(){};
                    Component component =  new Gson().fromJson(new_component.toString(), token.getType());

                    component.setStatus(status);

                    message.what = DataConstants.GET_COMPONENT_OPTION_SIGN;
                    message.obj = component;
                    handler.sendMessage(message);

                } catch (JSONException e) {
                    Toast.makeText(x.app(), "解析数据错误", Toast.LENGTH_SHORT).show();
                }

            }
        });

    }


    public static void getBuyData(Context context, final Handler handler) {

        RequestParams params = new RequestParams(BASE_SERVER_URL+ NetApi.BUY_COMPONENT_RESULT);


        XHttpTool.getDataByPost(params, new RequestListener() {
            @Override
            public void onSuccess(String result) {

                Message message = handler.obtainMessage();

                try {

                    Log.e("hehe" , "buyData ------>> " + result);

                    JSONObject json = new JSONObject(result);

                    int status = json.getInt("status");

                    JSONArray new_component = json.getJSONArray("short_componentlist");

                    TypeToken<List<Component>> token = new TypeToken<List<Component>>(){};
                    List<Component> components =  new Gson().fromJson(new_component.toString(), token.getType());

                    //component.setStatus(status);

                    message.what = DataConstants.BUY_COMPONENT_SIGN;
                    message.obj = components;
                    handler.sendMessage(message);

                } catch (JSONException e) {
                    Toast.makeText(x.app(), "解析数据错误", Toast.LENGTH_SHORT).show();
                }

            }
        });

    }

    /**
     * 获取历史记录
     * @param context
     * @param handler
     */
    public static void getHistoryData(Context context, final Handler handler) {

        RequestParams params = new RequestParams(BASE_SERVER_URL+ NetApi.HISTORY_COMPONENT_RESULT);

        XHttpTool.getDataByPost(params, new RequestListener() {
            @Override
            public void onSuccess(String result) {

                Message message = handler.obtainMessage();

                try {

                    JSONObject json = new JSONObject(result);

//                    int status = json.getInt("status");

                    JSONArray new_component = json.getJSONArray("historylist");

                    TypeToken<List<History>> token = new TypeToken<List<History>>(){};

                    List<History> historyList =  new Gson().fromJson(new_component.toString(), token.getType());

                    message.what = DataConstants.BUY_COMPONENT_SIGN;
                    message.obj = historyList;
                    handler.sendMessage(message);

                } catch (JSONException e) {
                    Toast.makeText(x.app(), "解析数据错误", Toast.LENGTH_SHORT).show();
                }

            }
        });

    }


    public static void onekeyOPen(final Context context , final Handler handler  , final String json , final String module_id){

        RequestParams params = new RequestParams(BASE_SERVER_URL+ "onekey_open");

        params.addBodyParameter("onekey", json);

        params.addBodyParameter("module_id", module_id);

        XHttpTool.getDataByPost(params, new RequestListener() {

            @Override
            public void onSuccess(String result) {

                Message message = handler.obtainMessage();

                Log.e("onekeyOPen", "onekeyOPen ------- >> " + result + " ---- " + json + " --- " + module_id);
                try {

                    JSONObject json = new JSONObject(result);

                    int status = json.getInt("status");

                    message.what = DataConstants.MODULE_OPEN_CABINET;
                    message.obj = status;
                    handler.sendMessage(message);

                } catch (JSONException e) {
                    Toast.makeText(x.app(), "解析数据错误", Toast.LENGTH_SHORT).show();
                }

            }
        });

    }


}
